﻿using Help;
using Model;
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;

namespace BLLA
{
    public partial class Order_ShoppingCar_T : BaseBLL<Model.Order_ShoppingCar_T>, IBLL.IOrder_ShoppingCar_TBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();

        /// <summary>
        /// 商品加入购物车
        /// </summary>
        /// <param name="OperID"></param>
        /// <param name="DisheID"></param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string Order_ShoppingCar_Add(int OperID, string DisheID, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 执行操作
            try
            {
                int ret = -1;
                Model.Order_ShoppingCar_T ls = new Model.Order_ShoppingCar_T();
                if (base.DBSession.IOrder_ShoppingCar_TDAL.GetListBy(x => x.DisheID == DisheID && x.UserID == OperID).Count > 0)
                {                    
                    ls = base.GetListBy(u => u.DisheID == DisheID && u.UserID == OperID).First();
                    ls.Num += 1;
                    ls.UpdateTime = DateTime.Now.ToString("yyyyMMddHHmmss");
                    ret = base.Modify(ls);
                }
                else
                {
                    ls.CarID = GetSysKey.GetKey("ShoppingCarKey");
                    ls.UserID = OperID;
                    ls.DisheID = DisheID;
                    ls.Num = 1;
                    ls.Status = 1;
                    ls.CreateTime = DateTime.Now.ToString("yyyyMMddHHmmss");
                    ret = base.Add(ls);
                }
                if (ret > 0)
                {
                    // 将操作记录到日志表中
                    Model.Order_OperationFlow_T Log = new Model.Order_OperationFlow_T();
                    Log.OperationTitle = "添加商品购物车信息";
                    Log.OperationText = "添加商品购物车信息成功";
                    Log.OperationType = "1F0006";
                    Log.OperationRes = "成功";
                    Log.OperationUser = OperID;
                    string str_log = os.StringOfObject(Log, RETID);
                    bool isSuccess = new Order_OperationFlow_T().AddOrder_OperationFlow(str_log, RETID);
                    if (!isSuccess)
                    {
                        WriteLog.WriteW("新增系统操作流水失败");
                    }
                    result.BS = "1";
                    result.Msg = "添加成功";
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "添加失败";
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("添加商品购物车信息失败：" + ex.Message);
                result.BS = "-1";
                result.Msg = "添加失败";
            }
            #endregion
            return os.StringOfObject(result, RETID);
        }

        /// <summary>
        /// 更新商品购物车信息
        /// </summary>
        /// <param name="CarID"></param>
        /// <param name="Num"></param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string Order_ShoppingCar_Update(string CarID, int Num, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 执行操作
            try
            {
                int ret = -1;
                Model.Order_ShoppingCar_T tmp = base.GetListBy(u => u.CarID == CarID).First();
                tmp.Num = Num;
                tmp.UpdateTime = DateTime.Now.ToString("yyyyMMddHHmmss");
                ret = base.Modify(tmp);
                if (ret > 0)
                {
                    result.BS = "1";
                    result.Msg = "更新成功";
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "更新失败";
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("更新商品购物车信息失败：" + ex.Message);
                result.BS = "-1";
                result.Msg = "更新失败";
            }
            #endregion
            return os.StringOfObject(result, RETID);
        }

        /// <summary>
        /// 购物车删除商品（可批量）
        /// </summary>
        /// <param name="CarID"></param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string Order_ShoppingCar_Del(string CarID, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 执行操作
            try
            {
                int ret = 0;
                //批量删除，去掉分隔符(,)前面的空数据
                if (CarID.Contains(','))
                {
                    CarID = CarID.Remove(0, 1);
                }
                string[] CarIDInfo = CarID.Split(',');
                if (CarIDInfo.Count() > 0)
                {
                    foreach (var item in CarIDInfo)
                    {
                        List<Model.Order_ShoppingCar_T> list_tmp = base.GetListBy(u => u.CarID == item);
                        if (list_tmp != null && list_tmp.Count > 0)
                        {
                            Model.Order_ShoppingCar_T tmp = list_tmp.First();
                            ret = base.Del(tmp);
                        }
                        ret += 1;
                    }
                }
                if (ret > 1)
                {
                    result.BS = "1";
                    result.Msg = "删除成功";
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "删除失败";
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("删除商品购物车信息失败：" + ex.Message);
                result.BS = "-1";
                result.Msg = "删除失败";
            }
            #endregion
            return os.StringOfObject(result, RETID);
        }

        /// <summary>
        /// 获取购物车信息
        /// </summary>
        /// <param name="OperID"></param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string Get_Order_Dishes_ShoppingCar(int OperID, string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion
                List<Model.Order_ShoppingCar_T> ls = new List<Model.Order_ShoppingCar_T>();
                ls = base.GetListBy(x => x.UserID == OperID).OrderBy(x => x.CarID).ToList();
                var reg = from m in ls
                          select new
                          {
                              m.CarID,
                              m.UserID,
                              m.DisheID,
                              m.Num,
                              m.Status,
                              m.CreateTime,
                              m.UpdateTime,
                              m.Order_Dishes_T.DishesID,
                              m.Order_Dishes_T.DishesNmae,
                              m.Order_Dishes_T.DishesPay,
                              m.Order_Dishes_T.DishesMon,
                              m.Order_Dishes_T.DishesTxt,
                              m.Order_Dishes_T.DishesPicSmall,
                          };
                if (reg != null & reg.Count() > 0)
                {
                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.rows = reg;
                    result.total = reg.Count();
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("获取商品信息列表失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }
    }
}
